package com.driving.single.intercepter.service.impl;

import com.driving.single.common.entity.LoginResult;
import com.driving.single.intercepter.service.UserLoginCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserLoginCheckServiceImpl implements UserLoginCheckService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Value("${redis_key_sso_user}")
    private String redisKeySsoUser;

    /**
     * 如果用户 token 不为空，则校验用户在 redis 中是否失效
     *
     * @param token 用户令牌
     */
    @Override
    public LoginResult checkUserToken(String token, String uid) {
        String key = this.redisKeySsoUser + ":" + uid;
        LoginResult loginResult = (LoginResult) this.redisTemplate.opsForValue().get(key);
        if (loginResult != null && token.equals(loginResult.getToken())) {
            return loginResult;
        }
        return null;
    }
}
